 //
//  FileCollectionCell.m
//  ICrEAM
//
//  Created by mac on 16/11/18.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "FileCollectionCell.h"
#import "MCDownloadManager.h"
#import "QKYDelayButton.h"
#import "MBProgressHUD+MJ.h"
@interface FileCollectionCell()
@property (weak, nonatomic) IBOutlet UIImageView *fileImage;
@property (weak, nonatomic) IBOutlet UILabel *fileNameT;
@property (weak, nonatomic) IBOutlet QKYDelayButton *button;

@property (weak, nonatomic) IBOutlet UIProgressView *progress;
@end

@implementation FileCollectionCell
- (IBAction)downLoadClick:(id)sender {
    
    MCDownloadReceipt *receipt = [[MCDownloadManager defaultInstance] downloadReceiptForURL:self.fileModel.filePath];
    
    if(receipt.state == MCDownloadStateCompleted||self.fileModel.isDir){
        if ([self.delegate respondsToSelector:@selector(FileCollectionCell:didDownLoadCompleteIsDir:)]) {
            [self.delegate FileCollectionCell:self didDownLoadCompleteIsDir:self.fileModel.isDir];
        }
    }
    else if (receipt.state == MCDownloadStateDownloading) {
        
        [[MCDownloadManager defaultInstance] suspendWithDownloadReceipt:receipt];
    }else {
        
        [self download];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.progress.progress = 0;
    self.button.clickDurationTime = 1.0;
    // Initialization code
}
-(void)setFileModel:(FileDirRespose *)fileModel{
    _fileModel=fileModel;
    self.fileNameT.text=fileModel.fileName;
    if (fileModel.isDir) {
        self.progress.hidden=YES;
        self.fileImage.image=[UIImage imageNamed:@"filrDir_img"];
    }else{
        self.progress.hidden=NO;
        NSString *imageStr;
        switch (fileModel.documentType) {
            case WordType:
                imageStr=[ICrEAMTool isVW]?@"word":@"word_sk";
                break;
            case PdfType:
                imageStr=[ICrEAMTool isVW]?@"pdf":@"pdf_sk";
                break;
            case ExcelType:
                imageStr=[ICrEAMTool isVW]?@"excel":@"excel_sk";
                break;
            case PptType:
                imageStr=[ICrEAMTool isVW]?@"ppt":@"ppt_sk";
                break;
            case ImageType:
                   imageStr=[ICrEAMTool isVW]?@"out_image":@"out_image";
                
                break;
                
            default:
             imageStr=[ICrEAMTool isVW]?@"filr_img":@"filr_img_sk";
                break;
        }
        self.fileImage.image=[UIImage imageNamed:imageStr];
    }

    
    MCDownloadReceipt *receipt = [[MCDownloadManager defaultInstance] downloadReceiptForURL:fileModel.filePath];
    
    self.progress.progress = receipt.progress.fractionCompleted;
    
    if (receipt.state == MCDownloadStateDownloading) {
        //        [self.button setTitle:@"停止" forState:UIControlStateNormal];
    }else if (receipt.state == MCDownloadStateCompleted) {
        //        [self.button setTitle:@"播放" forState:UIControlStateNormal];
    }else {
        //        [self.button setTitle:@"下载" forState:UIControlStateNormal];
    }
}
- (void)download {
    [[MCDownloadManager defaultInstance] downloadFileWithURL:self.fileModel.filePath
                                                    progress:^(NSProgress * _Nonnull downloadProgress, MCDownloadReceipt *receipt) {
                                                        
                                                        if ([receipt.url isEqualToString:self.fileModel.filePath]) {
                                            
                                                            self.progress.progress= downloadProgress.fractionCompleted;
                                                            
                                                        }
                                                        
                                                    }
                                                 destination:nil
                                                     success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSURL * _Nonnull filePath) {
                                                         
                                                         
                                                     }
                                                     failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
                                                         [MBProgressHUD showError:@"抱歉，该文件暂时无法预览"];
                                                     }];
    
}
@end
